package com.wisdom.smartbook.utils;

import android.app.Activity;

import java.util.Stack;


/**
 * activity管理工具类
 */
public class ActivityManagerUtils {

    private static Stack<Activity> sActivityStack;
    private volatile static ActivityManagerUtils sInstance;

    private ActivityManagerUtils() {
    }

    /**
     * 单例
     */
    public static ActivityManagerUtils getInstance() {
        if (sInstance == null) {
            synchronized (ActivityManagerUtils.class) {
                if (sInstance == null) {
                    sInstance = new ActivityManagerUtils();
                    sActivityStack = new Stack<>();
                }
            }
        }
        return sInstance;
    }

    /**
     * 添加Activity到堆栈
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        KLog.d(activity.getLocalClassName());
        if (sActivityStack == null) {
            sActivityStack = new Stack<>();
        }
        sActivityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     *
     * @return
     */
    public Activity getCurrentActivity() {
        try {
            return sActivityStack.lastElement();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前Activity的前一个Activity
     *
     * @return
     */
    public Activity getPreviousActivity() {
        int index = sActivityStack.size() - 2;
        if (index < 0) {
            return null;
        }
        return sActivityStack.get(index);
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        finishActivity(getCurrentActivity());
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {

        if (activity != null) {
            KLog.d(activity.getLocalClassName());
            sActivityStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 移除指定的Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        KLog.d(activity.getLocalClassName());
        if (activity != null) {
            sActivityStack.remove(activity);
        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls) {
        try {
            for (Activity activity : sActivityStack) {
                if (activity != null && activity.getClass().equals(cls)) {
                    finishActivity(activity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (sActivityStack == null) {
            return;
        }
        for (int i = 0, size = sActivityStack.size(); i < size; i++) {
            if (null != sActivityStack.get(i)) {
                sActivityStack.get(i).finish();
            }
        }
        sActivityStack.clear();
    }

    /**
     * 返回到指定的activity
     *
     * @param cls
     */
    public void returnToActivity(Class<?> cls) {
        while (sActivityStack.size() != 0) {
            if (sActivityStack.peek().getClass() == cls) {
                break;
            } else {
                finishActivity(sActivityStack.peek());
            }
        }
    }

    /**
     * 结束除当前传入以外所有 Activity
     *
     * @param cls
     */
    public void finishOthersActivity(Class<?> cls) {
        if (sActivityStack != null && sActivityStack.size() > 0) {
            for (Activity activity : sActivityStack) {
                if (activity != null && !activity.getClass().equals(cls)) {
                    activity.finish();
                }
            }
        }
    }

    /**
     * 是否已经打开指定的activity
     *
     * @param cls
     * @return true: 打开 false: 未打开
     */
    public boolean isOpenActivity(Class<?> cls) {
        if (sActivityStack != null) {
            for (int i = 0, size = sActivityStack.size(); i < size; i++) {
                if (cls == sActivityStack.peek().getClass()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断某activity是否处于栈顶
     *
     * @param cls
     * @return true: 打开 false: 未打开
     */
    public boolean isTopActivity(Class<?> cls) {
        if (sActivityStack != null && sActivityStack.size() > 0) {
            return cls == sActivityStack.lastElement().getClass();
        }
        return false;
    }

    public int getActivityCount() {
        if (sActivityStack == null) {
            return 0;
        }
        return sActivityStack.size();
    }

    /**
     * 退出应用程序
     */
    public void exitApp() {
        try {
            //杀死该应用进程
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
            finishAllActivity();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}